﻿<?php

include("inc_configuration.php");

$tid=0;
$t=0;
$y=0;
$yr=0;
$r=0;
$anz=0;

	$tQ = "	
				SELECT

				p.id AS playerId, 
				p.firstname AS fname, 
				p.name AS name, 
				p.teamId AS teamid

				FROM 

				$tbl_teams t,
				$tbl_players p,
				$tbl_events e

				WHERE 

				p.teamId=t.id AND
				
				t.id=$teamId
				
				GROUP BY p.id
				";

//echo $tQ;e.playerId=p.id AND

$tQe = mysql_query($tQ);

$platz=1;

while ($row = mysql_fetch_object($tQe))
{
	$playerId=$row->playerId;
	$pName=$row->name;
	$pfName=$row->fname;
	$tid=$row->teamid;
	
	$n=$pfName . " " . $pName;
	
	$player = array(
			  playerId => $playerId,
			  name => $n,
			  tID => $tid,
			  t => 0,
			  et => 0,
			  p =>0,
			  y => 0,
			  yr => 0,
			  r => 0);
	
	$players[] = $player;
	$anz++;
	
}


	$mQ = "	
			SELECT

			p.id AS playerId, 
			p.firstname AS fname, 
			p.name AS name, 
			p.teamId AS teamid,
			e.type,
			COUNT(*) AS anz

			FROM 

			$tbl_teams t,
			$tbl_players p,
			$tbl_events e,
			$tbl_leagues l,
			$tbl_season s,
			$tbl_matches m

			WHERE 
			e.matchId=m.id AND
			m.leagueId=l.id AND
			l.seasonId=s.id AND
			s.active=1 AND
			p.teamId=t.id AND
			e.playerId=p.id AND
			p.teamId=$teamId
			
			GROUP BY playerId,type
			";


// echo $mQ;

			
$mQe = mysql_query($mQ);

while ($row = mysql_fetch_object($mQe))
{
	
	$playerId=$row->playerId;
	$type=$row->type;
	$tanz=$row->anz;	
	
	$count=0;
	$found=0;
	
	while (($count<$anz) AND ($found==0))
	{	
		$player=$players[$count];
		
		$pI=$player["playerId"];
		
		if ($pI==$playerId)
		{
			
			
			if ($type==1)
			{
				$player[t] = $player[t]+$tanz;
				$found=1;
			}
			else
				{
					if ($type==2)
					{
						$player[y] = $player[y]+$tanz;
						$found=1;
					}
					else
						{
							if ($type==3)
							{
								$player[yr] = $player[yr]+$tanz;
								$found=1;
							}
							else
								{
									if ($type==4)
									{
										$player[r] = $player[r]+$tanz;
										$found=1;
									}
									else
									{
										if ($type==5)
										{
											$player[t] = $player[t]+$tanz;
											$player[p] = $player[p]+$tanz;
											$found=1;
										}
										else
										{
											if ($type==6)
											{
												$player[et] = $player[et]+$tanz;
												$found=1;
											}
										}
									}
								}
						}
				}
			$players[$count]=$player;
		}	
		
		$count++;
	
	
	}
}

	
$count=0;
for($count = 0; $count < $anz; $count++)
{
	$pA=$players[$count];
	$userId=$jUser;
	$pn=$pA[name];
	$tid=$pA[tID];
	$t=$pA[t];
	$p=$pA[p];
	$y=$pA[y];
	$yr=$pA[yr];
	$r=$pA[r];
	$et=$pA[et];
	$pid=$pA[playerId];
	
	$tableQ="INSERT INTO 	$tbl_statistics (userId,playerId,name,teamid,t,p,y,yr,r,et) 
							VALUES ($userId,$pid,'$pn',$tid,$t,$p,$y,$yr,$r,$et)";
							
	$tableQe=mysql_query($tableQ);
	
	
}


$tsQ="	SELECT s.userId,s.playerId,s.name,s.teamid,s.t,s.p,s.y,s.yr,s.r,s.et,p.rating,p.position
		FROM $tbl_statistics s, $tbl_players p
		WHERE
			s.userId=$jUser AND
			s.playerId=p.id
		ORDER BY position DESC";
$tsQe=mysql_query($tsQ);

echo "<center><table>";

echo "	<thead id='tabelle'>
				<tr>		
					<th>Position</th>
					<th>Spieler</th>
					<th>Stärke</th>
					<th>Tore (Elfmeter)</th>
					<th>Eigentore</th>
					<th>Gelb</th>
					<th>gelb-rot</th>
					<th>rot</th>
				</tr>";
				



$gest=0;

	while ($row = mysql_fetch_object($tsQe))
	{
		$p=0;
		$pN=$row->name;
		$tid=$row->teamid;
		$t=$row->t;
		$p=$row->p;
		$y=$row->y;
		$yr=$row->yr;
		$r=$row->r;
		$et=$row->et;
		$pid=$row->playerId;
		
		$gest=$gest+$t;
		
		$rating=$row->rating;
		
		$rc="r1";
		
		if ($rating<=50)
		{
			$rc="r1";
		}
		else
		{
			if ($rating<=65)
			{
				$rc="r2";
			}
			else
			{
				if ($rating<=79)
				{
					$rc="r3";
				}
				else
				{
					if ($rating<=89)
					{
						$rc="r4";
					}
					else
					{
						$rc="r5";
					}
				}
			}	
		}
		
		$pos = $row->position;		
		if ($pos==1)
		{	
			$posn="Tor";
		}
		else
		{
			if ($pos==2)
			{
				$posn="Abwehr";
			}
			else
			{
				if ($pos==3)
				{
					$posn="Mittelfeld";
				}
				else
				{
					if ($pos==4)
					{
						$posn="Sturm";
					}
				}
			
			}
		
		}
		
		echo "	<tr>
					<td>$posn</td>
					<td>$pN</td>
					<td class='$rc'>$rating</td>
					<td>$t ($p)</td>
					<td>$et</td>
					<td>$y</td>
					<td>$yr</td>
					<td>$r</td>
				</tr>";
		$platz++;
		
	}
	
	echo "<tr id='tabellenkopf' heigth='5px'>
			<th colspan='8'>Gesamt: $gest Tore</th></tr></table></center>";
	
	$l = "DELETE FROM $tbl_statistics WHERE userId=$jUser";
	$le = mysql_query($l);


?>